package com.itany.nmms.service.proxy;

import com.itany.mvc.util.CommonsMultipartFile;
import com.itany.nmms.exception.FileUploadErrorException;
import com.itany.nmms.exception.ProductExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.ProductService;
import com.itany.nmms.tran.TransactionManager;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 石小俊
 * @date 2023年11月10日 10:57
 */
public class ProductServiceProxy implements ProductService {
    @Override
    public void addProduct(String name, String price, List<CommonsMultipartFile> files, String typeId, HttpSession session) throws RequestParameterErrorException, ProductExistException, FileUploadErrorException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductService productService = (ProductService) ObjectFactory.getObject("productServiceTarget");
        try {
            tran.begin();
            productService.addProduct(name, price, files, typeId, session);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ProductExistException e) {
            tran.rollback();
            throw e;
        } catch (FileUploadErrorException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }
}
